package com.ruoyi.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.entityExcel.entityAJ;
import com.ruoyi.system.domain.entityExcel.entityGC;
import com.ruoyi.system.mapper.BAnjuanMapper;
import com.ruoyi.system.mapper.BJDaflglMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BGongchengMapper;
import com.ruoyi.system.service.IBGongchengService;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * 实体工程信息表Service业务层处理
 * 
 * @author zzr
 * @date 2023-09-18
 */
@Service
public class BGongchengServiceImpl implements IBGongchengService 
{
    @Autowired
    private BGongchengMapper bGongchengMapper;

    @Autowired
    private BAnjuanMapper bAnjuanMapper;

    @Autowired
    private BJDaflglMapper bjDaflglMapper;

    /**
     * 查询实体工程信息表
     * 
     * @param id 实体工程信息表主键
     * @return 实体工程信息表
     */
    @Override
    public BGongcheng selectBGongchengById(String id)
    {
        return bGongchengMapper.selectBGongchengById(id);
    }

    /**
     * 查询实体工程信息表列表
     * 
     * @param bGongcheng 实体工程信息表
     * @return 实体工程信息表
     */
    @Override
    public List<BGongcheng> selectBGongchengList(BGongcheng bGongcheng)
    {
        return bGongchengMapper.selectBGongchengList(bGongcheng);
    }

    @Override
    public ResponseEntity<byte[]> getTemplate(){
        try{
//            File picByte = new File("ruoyi-admin\\src\\main\\resources\\dataApparatusCheckScheme\\importTemplate.xlsx");

            String filePath = "dataApparatusCheckScheme/importTemplate.xlsx";
            // 使用相对类路径加载资源文件
            Resource resource = new ClassPathResource(filePath);
            // 获取资源文件的 InputStream
            InputStream inputStream = resource.getInputStream();
            // 将 InputStream 转为 byte 数组
            byte[] picBytes = StreamUtils.copyToByteArray(inputStream);

//            byte[] picBytes = Files.readAllBytes(picByte.toPath());
            return ResponseEntity.ok().header("Content-Type","application/excel").body(picBytes);
        }catch (IOException e){
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 新增实体工程信息表
     * 
     * @param bGongcheng 实体工程信息表
     * @return 结果
     */
    @Override
    public int insertBGongcheng(BGongcheng bGongcheng)
    {
        return bGongchengMapper.insertBGongcheng(bGongcheng);
    }

    /**
     * 修改实体工程信息表
     * 
     * @param bGongcheng 实体工程信息表
     * @return 结果
     */
    @Override
    public int updateBGongcheng(BGongcheng bGongcheng)
    {
        return bGongchengMapper.updateBGongcheng(bGongcheng);
    }

    /**
     * 批量删除实体工程信息表
     * 
     * @param ids 需要删除的实体工程信息表主键
     * @return 结果
     */
    @Override
    public int deleteBGongchengByIds(String[] ids)
    {
        return bGongchengMapper.deleteBGongchengByIds(ids);
    }

    /**
     * 删除实体工程信息表信息
     * 
     * @param id 实体工程信息表主键
     * @return 结果
     */
    @Override
    public int deleteBGongchengById(String id)
    {
        return bGongchengMapper.deleteBGongchengById(id);
    }

    // 查询根据根据年度获得统计
    @Override
    public List<TongJiTemp> getNianDuTongJi(){
        return bGongchengMapper.getNianDuTongJi();
    }

    // 查询根据分类获得统计
    @Override
    public List<TongJiTemp> getFenLeiTongJi(){
        return bGongchengMapper.getFenLeiTongJi();

    }

    @Override
    public String importGCExcel(List<entityGC> gc, List<entityAJ> aj, Long classId) {
        BGongcheng bGongcheng = this.creatBGC(classId);
        entityGC GC = gc.get(0);
        BGongcheng finalGc = this.transferBGC(bGongcheng,GC);
        finalGc.setYuliuA(Long.valueOf(aj.size()));
        bGongchengMapper.insertBGongcheng(finalGc);

        for (entityAJ AJ : aj){
            BAnjuan bAnjuan = this.createBAJ(bGongcheng);
            BAnjuan finalAj = this.transferBAJ(bAnjuan, AJ);
            bAnjuanMapper.insertBAnjuan(finalAj);
        }

        return "导入完成";
    }

    // 导入中初始化一个BGongcheng
    public BGongcheng creatBGC(Long classId){
        BGongcheng bGongcheng = new BGongcheng();
        bGongcheng.setId(UUID.randomUUID().toString());
        bGongcheng.setDangAnGuanName("宝鸡市城建档案馆");
        bGongcheng.setDangAnGuanNo("461403");
        bGongcheng.setQuanZongNo("CJDA");

        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();
        bGongcheng.setCreationTime(currentDate);
        // 分类
        BJDaflgl bjDaflgl = bjDaflglMapper.selectBJDaflglById(classId);
        bGongcheng.setDaLeiNo(String.valueOf(bjDaflgl.getNo().charAt(0)));
        bGongcheng.setShuLeiNo(String.valueOf(bjDaflgl.getNo().charAt(1)));
        bGongcheng.setXiaoLeiNo(bjDaflgl.getNo().substring(2));

        bGongcheng.setDaLeiName(bjDaflgl.getName());
        BJDaflgl bjDaflgl_F = bjDaflglMapper.selectBJDaflglById(bjDaflgl.getFid());
        bGongcheng.setShuLeiName(bjDaflgl_F.getName());
        bGongcheng.setXiaoLeiName(bjDaflglMapper.selectBJDaflglById(bjDaflgl_F.getFid()).getName());

        // 状态
        bGongcheng.setProjectStatus(0L);

        return bGongcheng;
    }

    // 将entityGC的值传入BGongcheng
    public BGongcheng transferBGC(BGongcheng bGongcheng, entityGC GC){
        bGongcheng.setEngName(GC.getEngName());
        bGongcheng.setSecretLevel(GC.getSecretLevel());
        bGongcheng.setStorageLife(GC.getStorageLife());
        bGongcheng.setAnJuanShu(GC.getAnJuanShu());
        bGongcheng.setBianZhiDateStart(GC.getBianZhiDateStart());
        bGongcheng.setBianZhiDateEnd(GC.getBianZhiDateEnd());
        bGongcheng.setXingChengNianDu(GC.getXingChengNianDu());
        bGongcheng.setYiJiaoDanWei(GC.getYiJiaoDanWei());
        bGongcheng.setEngCost(GC.getEngCost());
        bGongcheng.setEngAddress(GC.getEngAddress());
        bGongcheng.setBeiZhu(GC.getBeiZhu());
        return bGongcheng;
    }

    // 导入中初始化一个BAnjuan
    public BAnjuan createBAJ(BGongcheng bGongcheng){
        BAnjuan bAnjuan = new BAnjuan();
        bAnjuan.setId(UUID.randomUUID().toString());
        bAnjuan.setAnJuanName(bGongcheng.getEngName() + "-");
        bAnjuan.setGid(bGongcheng.getId());
        bAnjuan.setGongChengName(bGongcheng.getEngName());
        bAnjuan.setSecretLevel(bGongcheng.getEngAddress());
        bAnjuan.setStorageLife(bGongcheng.getStorageLife());

        Map<String,String> transfer = symbolToName();
        bAnjuan.setSecretLevelName(transfer.get(bAnjuan.getSecretLevel()));
        bAnjuan.setStorageLifeName(transfer.get(bAnjuan.getStorageLife()));

        bAnjuan.setFenLeiNo(bGongcheng.getDaLeiNo() + bGongcheng.getShuLeiNo() + bGongcheng.getXiaoLeiNo());
        if(bAnjuanMapper.selectLastZongJuanNo() != null){
            bAnjuan.setZongJuanNo(bAnjuanMapper.selectLastZongJuanNo() + 1);
        }
        else {
            bAnjuan.setZongJuanNo(1L);
        }
        bAnjuan.setBianZhiDate(bGongcheng.getBianZhiDateEnd());

        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();
        bAnjuan.setCreationTime(currentDate);
        if(bAnjuanMapper.getClassAJNo(bAnjuan.getFenLeiNo()) != null){
            bAnjuan.setAnJuanNo(bAnjuanMapper.getClassAJNo(bAnjuan.getFenLeiNo()) + 1);
        }
        else {
            bAnjuan.setAnJuanNo(1L);
        }

        // 获取juanno
        BAnjuan temperAujuan = new BAnjuan();
        temperAujuan.setGid(bGongcheng.getId());
        List<BAnjuan> aujuanList = bAnjuanMapper.selectBAnjuanList(temperAujuan);
        Long numOfAnjuan = Long.valueOf(aujuanList.size());
        bAnjuan.setJuanNo(numOfAnjuan + 1);

        bAnjuan.setDangNo(bGongcheng.getXingChengNianDu() + '-' + bAnjuan.getFenLeiNo() + '-'
                          + bAnjuan.getStorageLife() + '-' + bAnjuan.getAnJuanNo());

        return bAnjuan;
    }

    // 将entityAJ的值传入BAnjuan
    public BAnjuan transferBAJ(BAnjuan bAnjuan, entityAJ AJ){
        bAnjuan.setAnJuanName(bAnjuan.getAnJuanName() + AJ.getAnJuanName());
        bAnjuan.setXingChengDanWei(AJ.getXingChengDanWei());
        bAnjuan.setLanTuZhangShu(AJ.getLanTuZhangShu());
        bAnjuan.setDiTuZhangShu(AJ.getDiTuZhangShu());
        bAnjuan.setWenZiCaiLiaoZhangShu(AJ.getWenZiCaiLiaoZhangShu());
        bAnjuan.setKuNum(AJ.getKuNum());
        bAnjuan.setLeiNum(AJ.getLeiNum());
        bAnjuan.setJiaNum(AJ.getJiaNum());
        bAnjuan.setCengShu(AJ.getCengShu());
        return bAnjuan;
    }

    public Map<String,String> symbolToName(){
        Map<String,String> dictionary = new HashMap<>();
        dictionary.put("GK","公开");
        dictionary.put("GN","国内");
        dictionary.put("NB","内部");
        dictionary.put("MM","秘密");
        dictionary.put("JM","机密");
        dictionary.put("UM","绝密");
        dictionary.put("Y","永久");
        dictionary.put("C","长期");
        dictionary.put("D","短期");
        return dictionary;
    }
}
